package com.pojoin.cms.session;

import javax.servlet.http.HttpSession;

import com.pojoin.cms.verification.Verification;

public class SessionServerLocal implements SessionServer {

	private HttpSession httpSession;
	private Verification verification;

	private static String SESSION_USER_NAME = "SESSION_USER_NAME";

	public SessionServerLocal(HttpSession session, Verification verification) {
		this.httpSession = session;
		this.verification = verification;
	}

	@Override
	public HttpSession getSession() {
		return this.httpSession;
	}

	@SuppressWarnings("static-access")
	@Override
	public void logoutSession() {
		this.httpSession.removeAttribute(this.SESSION_USER_NAME);

	}

	@SuppressWarnings("static-access")
	@Override
	public boolean registerSession(String userName, String passWord) {
		if (userName != null && userName.equals(this.getUserNameFromSession())) {
			return true;
		}
		if (this.verification.verify(userName, passWord)) {
			this.httpSession.setAttribute(this.SESSION_USER_NAME, userName);
			return true;
		}
		return false;
	}

	@Override
	public String getUserNameFromSession() {
		return (String) this.httpSession.getAttribute(SESSION_USER_NAME);
	}

	@Override
	public boolean checkSession() {
		String userName = (String) this.httpSession
				.getAttribute(SESSION_USER_NAME);
		if (userName != null) {
			return true;
		}
		return false;
	}

}
